<?php
namespace Admin\Model;

use Admin\AppConst\WebConst;
use Think\Model;

//用户model
class BaseModel extends Model
{
    /**
     * @var BaseModel
     */
    private     static $instance;
    protected   $sex            = 'goods_class_type_id';
    private     $isDelete       = 'is_delete';
    protected   $field          = '*';


    /**
     * Get the instance of BaseModel.
     * @return BaseModel
     */
    public static function get()
    {
        if (!self::$instance) {
            self::$instance = new BaseModel();
        }
        return self::$instance;
    }


    /* @function    setDefaultWhere     设置默认的where条件
     * @param       $isCheckSex bool    是否增加性别where条件
     * @date        2018-07-25
     * @author      zhangle
     * @return      array
     */

    protected function setDefaultWhere($isCheckSex = WebConst::DEFAULT_TYPE_TRUE){
        $result     = WebConst::DEFAULT_TYPE_EMPTY_ARRAY;
        $result     = array($this->isDelete => WebConst::DEFAULT_TYPE_DELETE_FALSE);
        $sexList    = $this->getSexList();

        if(!empty($sexList) && $isCheckSex == WebConst::DEFAULT_TYPE_TRUE){
            $result[$this->sex]   = array('in', implode(',', $sexList));
        }

        return $result;
    }

    /* @function    getSex      //获取性别
     * @date        2018-07-25
     * @author      zhangle
     */

    private function getSexList(){
        $result     = WebConst::DEFAULT_TYPE_EMPTY_ARRAY;
        $sex        = intval(INIT_HEADER_NAME_SEX);
        $sexList    = WebConst::CATEGORY_TYPE_IN_LIST;

        if(isset($sexList[$sex]) && in_array($sex, WebConst::CATEGORY_TYPE_ALL)){
            $result = $sexList[$sex];
        }

        return $result;
    }

    /* @function    setFields   设置需要获取的字段
     * @param       $fieldList  array|string
     * @date        2018-07-25
     * @author      zhangle
     */

    protected function setFields($fieldList){
        if(is_array($fieldList)){
            $this->field        = implode(',', $fieldList);
        }

        $this->field    = $fieldList;
    }


    /* @function    getStartLimit   处理分页相关
     * @param       int             $page           页码  default 1
     * @param       int             $pageSize       条数  default 10
     * @date        2018-07-05
     * @author      zhangle
     * @return      array
     */

    protected function getStartLimit($page = WebConst::DEFAULT_TYPE_PAGE, $pageSize = WebConst::DEFAULT_TYPE_PAGE_SIZE){
        return array((($page - 1) * $pageSize), $pageSize);
    }

}